package com.payment.ktb.activity.main1;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.ConstantsPay;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/8.
 */

public class CashierUnionOnlineActivity extends BaseActivity {


    @BindView(R.id.tv_cashieruniononline_amount)
    TextView tv_cashieruniononline_amount;


    String linkId ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cashieruniononline);
        ButterKnife.bind(this);
        initTitle("银联快捷");

    }



    @Override
    protected void onResume() {
        super.onResume();
        linkId = String.valueOf(new Date().getTime());
    }

    @OnClick({R.id.btn_cashieruniononline_one,R.id.btn_cashieruniononline_two,R.id.btn_cashieruniononline_three,
            R.id.btn_cashieruniononline_four,R.id.btn_cashieruniononline_five,R.id.btn_cashieruniononline_six,
            R.id.btn_cashieruniononline_seven,R.id.btn_cashieruniononline_eight,R.id.btn_cashieruniononline_nine,R.id.btn_cashieruniononline_zero,
            R.id.rl_cashieruniononline_delete,R.id.btn_cashieruniononline_confirm,R.id.btn_cashieruniononline_deleteone,R.id.btn_cashieruniononline_dot})

    public void clickEvent(View view){

        switch (view.getId()) {


            //one
            case R.id.btn_cashieruniononline_one:
                handleMoney("1");
                break;
            //two
            case R.id.btn_cashieruniononline_two:
                handleMoney("2");
                break;
            //three
            case R.id.btn_cashieruniononline_three:
                handleMoney("3");
                break;
            //four
            case R.id.btn_cashieruniononline_four:
                handleMoney("4");
                break;
            //five
            case R.id.btn_cashieruniononline_five:
                handleMoney("5");
                break;
            //six
            case R.id.btn_cashieruniononline_six:
                handleMoney("6");
                break;
            //seven
            case R.id.btn_cashieruniononline_seven:
                handleMoney("7");
                break;
            //eight
            case R.id.btn_cashieruniononline_eight:
                handleMoney("8");
                break;
            //nine
            case R.id.btn_cashieruniononline_nine:
                handleMoney("9");
                break;
            //zero
            case R.id.btn_cashieruniononline_zero:
                handleMoney("0");
                break;
            //dot
            case R.id.btn_cashieruniononline_dot:
                handleMoney(".");
                break;
            //delete
            case R.id.rl_cashieruniononline_delete:
                if (!TextUtils.isEmpty(tv_cashieruniononline_amount.getText().toString()) && tv_cashieruniononline_amount.getText().toString().length() > 1) {
                    tv_cashieruniononline_amount.setText(tv_cashieruniononline_amount.getText().toString().substring(0,tv_cashieruniononline_amount.getText().length() - 1));
                } else if (!TextUtils.isEmpty(tv_cashieruniononline_amount.getText().toString()) && tv_cashieruniononline_amount.getText().toString().length() == 1) {
                    tv_cashieruniononline_amount.setText("0");
                }

                break;
            //delete one
            case R.id.btn_cashieruniononline_deleteone:
                tv_cashieruniononline_amount.setText("0");
                break;
            //confirm
            case R.id.btn_cashieruniononline_confirm:
                if (checkPass()) {
                    createOrder();
                }
                break;

        }



    }

    private boolean checkPass() {

        try {
            if (TextUtils.isEmpty(tv_cashieruniononline_amount.getText().toString())) {
                ToastUtils.showToast("请输入金额！");
                return false;
            }else if (Double.valueOf(tv_cashieruniononline_amount.getText().toString()) < 5) {
                ToastUtils.showToast("不得小于5元");
                return false;
            }
            return true;
        }catch (Exception e) {
            ToastUtils.showToast("请输入正确的金额");
            return false;
        }

    }

    private void createOrder() {

        progressDialogUtils.showProgress();

        //构造传输参数
        final Map<String, String> map = new HashMap<String,String>();
        map.put("linkId",linkId);
        map.put("payType","11");
        map.put("orderType","10");
        map.put("amount",Double.valueOf(tv_cashieruniononline_amount.getText().toString().trim())*100 + "");
        map.put("goodsName","商户收款");
        map.put("product", ConstantsPay.product1000);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.PayOrderCreate, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = new Intent(mContext,OrderConfirmActivity.class);
                    intent.putExtra("amount",Double.valueOf(tv_cashieruniononline_amount.getText().toString().trim())*100 + "");
                    intent.putExtra("rateFee",jsonObject.optString("rateFee"));
                    intent.putExtra("vipFlg",jsonObject.optString("vipFlg"));
                    intent.putExtra("rateNormalFee",jsonObject.optString("rateNormalFee"));
                    intent.putExtra("rateNormalFeeValue",jsonObject.optString("rateNormalFeeValue"));
                    intent.putExtra("rateVipFee",jsonObject.optString("rateVipFee"));
                    intent.putExtra("rateVipFeeValue",jsonObject.optString("rateVipFeeValue"));
                    intent.putExtra("vipDiscount",jsonObject.optString("vipDiscount"));
                    intent.putExtra("vouchersFlg",jsonObject.optString("vouchersFlg"));
                    intent.putExtra("vouchersPrice",jsonObject.optString("vouchersPrice"));
                    intent.putExtra("settleReal",jsonObject.optString("settleReal"));
                    intent.putExtra("bindCards",jsonObject.optString("bindCards"));
                    intent.putExtra("orderNo",jsonObject.optString("orderNo"));
                    intent.putExtra("vouchersFlg",jsonObject.optString("vouchersFlg"));
                    intent.putExtra("fromActivity","CashierUnionOnlineActivity");
                    startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });


    }


    /**
     *
     * @param amount
     */
    private void handleMoney(String amount) {
        String temp = tv_cashieruniononline_amount.getText().toString().trim();
        if(!TextUtils.isEmpty(temp) && temp.length() == 1 && temp.substring(0, 1).equals("0")){
            if (!amount.equals(".")){
                tv_cashieruniononline_amount.setText(amount);
            }
        } else if (temp.contains(".")){
            if (!amount.contains(".")) {
                if ((temp.length() - temp.indexOf(".")) > 2) {
                    AlertDialogUtils.showAlertDialog(mContext,"精确到分");
                }else if (Double.valueOf(temp + amount) > 50000){
                    AlertDialogUtils.showAlertDialog(mContext,"单笔金额超限，请输入较小金额");
                } else {
                    tv_cashieruniononline_amount.setText(temp + amount);

                }
            }
            }else {
            if (!amount.equals(".") && Double.valueOf(temp + amount) > 50000) {
                AlertDialogUtils.showAlertDialog(mContext,"单笔金额超限，请输入较小金额");
            }else {
                tv_cashieruniononline_amount.setText(temp + amount);
            }
        }

        }

}
